﻿Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Microsoft.Reporting.WebForms
Imports log4net

Partial Class Report_RP12
    Inherits System.Web.UI.Page

    Public ScriptText As String
    Dim Report As Object
    Private Shared logger As ILog = LogManager.GetLogger("Report_RP12")

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        'If Session("fundcenterS") = "" Then
        '    Session("fundcenterS") = "0011"
        '    Session("fundcenterE") = "0012"

        'End If

        'If Session("users") = "" Then
        '    Session("users") = "wassana"
        'End If
        'Session("year") = 2556
        'Session("version") = 2


        Getdata()


    End Sub


    Function Getdata() As Decimal


        Dim adapter As New Data_Report2TableAdapters.RP12TableAdapter
        Dim sql As String
        Dim dt As New Data_Report2.RP12DataTable


        sql = " SELECT [BudgetRevPlans].[Id],[BudgetRevPlans].[PlanYear],[BudgetRevPlans].[Version],[ActualLastYear2],[ActualLastYear1],[ForecastLastYear1]"
        sql += " ,[ForecastCurYear]   ,([ForecastCurYear]-[ForecastLastYear1]) as dif"
        sql += " , case isnull([ForecastLastYear1],0) when 0  then 0 else ((([ForecastCurYear]-[ForecastLastYear1]) / [ForecastLastYear1])* 100) end as percen"
        sql += " ,[LastYearMonth1] ,[LastYearMonth2],[LastYearMonth3],[LastYearMonth4],[LastYearMonth5],[LastYearMonth6],[LastYearMonth7],[LastYearMonth8]"
        sql += " ,[LastYearMonth9],[LastYearMonth10],[LastYearMonth11],[LastYearMonth12],[CurYearMonth1],[CurYearMonth2],[CurYearMonth3],[CurYearMonth4]"
        sql += " ,[CurYearMonth5],[CurYearMonth6],[CurYearMonth7],[CurYearMonth8],[CurYearMonth9],[CurYearMonth10],[CurYearMonth11],[CurYearMonth12]"
        sql += " ,[FundCenter_FundCenterCode],[FundCenter_PlanYear] ,FundCenters.Description AS FundCenterName,[Product_ProductCode],[Product_PlanYear]"
        sql += " ,Products.Description AS ProductName,[ProductItem_Id],ProductItems.ItemNo,ProductItems.Description AS ItemName "
        sql += " FROM [BudgetRevPlans]"
        sql += " INNER JOIN FundCenters ON [BudgetRevPlans].[FundCenter_FundCenterCode] = Fundcenters.FundCenterCode"
        sql += " AND [BudgetRevPlans].[FundCenter_PlanYear] = FundCenters.PlanYear"
        sql += " INNER JOIN Products ON [BudgetRevPlans].[Product_ProductCode] = Products.ProductCode"
        sql += " AND [BudgetRevPlans].[Product_PlanYear] = Products.PlanYear"
        sql += " INNER JOIN ProductItems ON [BudgetRevPlans].[ProductItem_Id] = ProductItems.Id"

        sql += " and  BudgetRevPlans.PlanYear  = " & Session("year") & " and BudgetRevPlans.Version = '" & Session("version") & "'"
        If Session("FCTRmode") = "1" Then
            sql += " and  BudgetRevPlans.FundCenter_FundCenterCode IN (" & Session("FundcenterAll") & ") "
        ElseIf Session("FCTRmode") = "2" Then
            sql += " and  BudgetRevPlans.FundCenter_FundCenterCode IN (" & Session("FundcenterAll") & ") "
        End If

        If Val(Session("ProductS")) <> 0 And Val(Session("ProductE")) <> 0 Then
            sql += "  and  Products.ProductCode between " & Session("ProductS") & " and " & Session("ProductE")
        End If



        sql += " ORDER BY Fundcenter_FundcenterCode, FormId, Product_ProductCode, ItemNo"


        'Response.Write(sql)
        'Exit Function
        adapter.GetDataByFilter(dt, sql)



        'Return 1
        'Exit Function
        dt.TableName = "R12DT"
        Dim das As New DataSet
        das.DataSetName = "R01DS"
        das.Tables.Add(dt)
        ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local
        'ReportViewer1.LocalReport.ReportPath = System.Environment.CurrentDirectory & "Report\R01.rdlc"
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", das.Tables("R12DT")))
        ReportViewer1.DocumentMapCollapsed = True

        Dim UserName As String = Session("users")
        Dim years As String = Session("year")
        Dim version As String = Session("version")
        Dim users As String = Session("users")

        Dim p0, p1, p2, p3 As ReportParameter
        p1 = New ReportParameter("year", years)
        p0 = New ReportParameter("year1", years - 1)
        p2 = New ReportParameter("version", version)
        p3 = New ReportParameter("username", UserName)
        ReportViewer1.LocalReport.SetParameters(p0)
        ReportViewer1.LocalReport.SetParameters(p1)
        ReportViewer1.LocalReport.SetParameters(p2)
        ReportViewer1.LocalReport.SetParameters(p3)


        ReportViewer1.LocalReport.Refresh()


        'Response.Write(dt.Rows.Count & sql)
        Session("count") = dt.Rows.Count



        Return dt.Rows.Count

    End Function


    Public Function nPageNumber() As String
        Dim str As String
        str = Me.Report.Globals!PageNumber.ToString()
        Return str
    End Function

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session("users") Is Nothing Then
            ScriptText = "<script> wclose();</script>"
        End If
    End Sub
End Class
